package com.sun.controller;

import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/** 根据提交的locale参数指定语言环境
 * @author sun
 */
public class MyLocale implements LocaleResolver {
    /**
     * Resolve the current locale via the given request.
     * Can return a default locale as fallback in any case.
     *
     * @param request the request to resolve the locale for
     * @return the current locale (never {@code null})
     */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String localeStr=request.getParameter("locale");
        System.out.println("locale="+localeStr);
        Locale locale = null;
        //如果有locale参数，按照locale参数设定信息
        if( localeStr!=null &&!"".equals(localeStr)){
            locale=new Locale(localeStr.split("_")[0],localeStr.split("_")[1]);
        }else {
            //没有就按浏览器配置的默认locale信息
            locale=request.getLocale();
        }
        return locale;
    }

    /**
     * Set the current locale to the given one.
     *
     * @param request  the request to be used for locale modification
     * @param response the response to be used for locale modification
     * @param locale   the new locale, or {@code null} to clear the locale
     * @throws UnsupportedOperationException if the LocaleResolver
     *                                       implementation does not support dynamic changing of the locale
     */
    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        throw new UnsupportedOperationException(
                "Cannot change HTTP accept header - use a different locale resolution strategy");
    }
}
